﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using T.Contract.Domain.T.Contract.Model;
using T.Contract.API.Application.Command.ContractCommand;
using T.ApiResults;
using Microsoft.AspNetCore.Authorization;

using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using System.Diagnostics.Contracts;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace T.Contract.API.Controllers
{  /// <summary>
   /// 写控制器
   /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
   
    public class ContractController : ControllerBase
    {
        protected readonly IMediator mediator;

        public ContractController(IMediator mediator)
        {
            this.mediator = mediator;
        }
       /// <summary>
        /// 添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddContract(ContractCreateCommand command)
        {
            ApiResult<int>result=new ApiResult<int>();
         try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                command.CreatedDate = DateTime.Now;
                result.Data=await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
         
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelContract(ContratDeleteCommand command)
        {
            ApiResult<int>result = new ApiResult<int>();

            try
            {
                result.Data = await mediator.Send(new ContratDeleteCommand()
                {
                ContractId=command.ContractId,
               DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取名称
               DeletedDate = DateTime.Now,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateContract(ContractTupdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                command.UpdatedDate = DateTime.Now;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 定义线路
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateLineCon([FromQuery]ContractLineAddCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
               
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateTStateContract(ContractTStateupdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(new ContractTStateupdateCommand()
                {
                    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取名称
                    UpdatedDate = DateTime.Now,
                    ContractId=command.ContractId,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
   
      
        /// <summary>
        /// 单文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult>Upfile(IFormFile file)
        {
            string path = Directory.GetCurrentDirectory() + @"/wwwroot/file/" + file.FileName;
            using (FileStream fs=new FileStream(path,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok(file.FileName);
        }
        /// <summary>
        /// 审批合同
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CrerateAppro(ApprovalRecorddCreateCommand command)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {

                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                command.CreatedDate = DateTime.Now;
                apiResult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiResult.Code = -1;
                apiResult.Message = ex.Message;
                throw;
            }
            return apiResult;
        }
    }
}
